package com.basemodel;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.io.Serializable;

/**
 * 请求返回结果
 *
 * @author cxt
 * @date 2022/4/2
 */
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
public class Result<T> implements Serializable {
    public static final String SAVE_SU = "保存成功！";
    public static final String DELETE_SU = "删除成功！";
    public static final String UPDATE_SU = "更新成功！";

    public static final String SAVE_FA = "保存失败！";
    public static final String DELETE_FA = "删除失败！";
    public static final String UPDATE_FA = "更新失败！";

    private Integer code;

    private String message;

    private T Data;

    /**
     * 返回成功消息
     * 一参
     */
    public static <T> Result<T> success(String msg) {
        return Result.success(msg, null);
    }

    /**
     * 返回成功消息
     * 一参
     */
    public static <T> Result<T> success(T data) {
        return new Result<>(200, "操作成功", data);
    }

    /**
     * 返回成功消息
     * 两参
     */
    public static <T> Result<T> success(String msg, T data) {
        return new Result<>(200, msg, data);
    }

    /**
     * 返回警告消息
     * 一参数
     */
    public static <T> Result<T> warn(String msg) {
        return Result.warn(401, msg);
    }

    /**
     * 返回警告消息
     * 两参数
     */
    public static <T> Result<T> warn(Integer code, String msg) {
        return Result.warn(code, msg, null);
    }

    /**
     * 返回警告消息
     * 三参数
     */
    public static <T> Result<T> warn(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }

    /**
     * 返回错误消息
     * 无参
     */
    public static <T> Result<T> error() {
        return Result.error("操作失败");
    }

    /**
     * 返回错误消息
     * 一参
     */
    public static <T> Result<T> error(String msg) {
        return Result.error(500, msg);
    }

    /**
     * 返回错误消息
     * 两参
     */
    public static <T> Result<T> error(Integer code, String msg) {
        return Result.error(code, msg, null);
    }

    /**
     * 返回错误消息
     * 两参
     */
    public static <T> Result<T> error(Integer code, String msg, T data) {
        return new Result<>(code, msg, data);
    }
}
